package com.hkbigdata.transfrom;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author liuanbo
 * @creat 2023-04-19-17:06
 * @see 2194550857@qq.com
 */
public class Flink01_TransForm_Map_RichMapFunction {
    public static void main(String[] args) throws Exception {
        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.数据源
        env.fromElements(1,2,3,4)
                .map(new MyRichMapFunction())
                .print();

        env.execute();
    }
    public static class MyRichMapFunction extends RichMapFunction<Integer,Integer>{

        @Override
        public Integer map(Integer value) throws Exception {
            System.out.println("每个元素执行一次");
            return value*value;
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            System.out.println("程序执行的时候加载一次");
        }

        @Override
        public void close() throws Exception {
            System.out.println("close执行一次");
        }
    }
}
